local DIY = require "packages.diy_utility.diy_utility"
local LB_U = require "packages.lb_kill_utility.utility"

local skel = fk.CreateSkill {
  name = "lb_zero__xuanmo",
  tags = { Skill.Switch },
}

Fk:loadTranslationTable {
  ["lb_zero__xuanmo"] = "玄墨",
  [":lb_zero__xuanmo"] = "<b>转换技</b>，你可以将一张黑色牌当雷【杀】使用，目标角色" ..
      "阳：结算期间视为装备【八卦阵】；" ..
      "阴：结算后依次进行【闪电】判定。" ..
      "结算后若本回合弃牌堆中红色与黑色牌数相等，则不计次数。",

  [":lb_zero__xuanmo_yang"] = "<b>转换技</b>，你可以将一张黑色牌当雷【杀】使用，目标角色" ..
      "<font color=\"#E0DB2F\">阳：结算期间视为装备【八卦阵】；</font>" ..
      "阴：结算后依次进行【闪电】判定。" ..
      "结算后若本回合弃牌堆中红色与黑色牌数相等，则不计次数。",

  [":lb_zero__xuanmo_yin"] = "<b>转换技</b>，你可以将一张黑色牌当雷【杀】使用，目标角色" ..
      "阳：结算期间视为装备【八卦阵】；" ..
      "<font color=\"#E0DB2F\">阴：结算后依次进行【闪电】判定。</font>" ..
      "结算后若本回合弃牌堆中红色与黑色牌数相等，则不计次数。",

  ["#lb_zero__xuanmo"] = "玄墨：你可以将一张黑色牌当雷【杀】使用",

  ["$lb_zero__xuanmo1"] = "阴阳相生。",
  ["$lb_zero__xuanmo2"] = "动静相宜。",
  ["$lb_zero__xuanmo3"] = "邪祟，诛灭！",
  ["$lb_zero__xuanmo4"] = "命数，该绝。",
}

skel:addEffect("viewas", {
  mute = true,
  anim_type = "offensive",
  pattern = "slash",
  prompt = "#lb_zero__xuanmo",
  filter_pattern = {
    min_num = 1,
    max_num = 1,
    pattern = ".|.|black",
  },
  view_as = function(self, player, cards)
    if #cards ~= 1 then return nil end
    local card = Fk:cloneCard("thunder__slash")
    card:addSubcards(cards)
    card.skillName = skel.name
    return card
  end,
  before_use = function(self, player, use)
    use.extra_data = use.extra_data or {}
    use.extra_data.lb_zero__xuanmo = player
    local state = player:getSwitchSkillState(skel.name, true)
    local tos = table.simpleClone(use.tos)
    local audio = math.random(2)
    use.extra_data.lb_zero__xuanmo_state = { state, tos }
    if state == fk.SwitchYang then
      for _, to in ipairs(tos) do
        LB_U.addVirtualEquip(to, "eight_diagram")
      end
    else
      audio = audio + 2
    end
    player.room:notifySkillInvoked(player, skel.name, "offensive")
    player:broadcastSkillInvoke(skel.name, audio)
  end,
  after_use = function(self, player, use)
    local room = player.room
    use.extra_data = use.extra_data or {}
    if use.extra_data.lb_zero__xuanmo == player and type(use.extra_data.lb_zero__xuanmo_state) == "table" then
      local state, tos = table.unpack(use.extra_data.lb_zero__xuanmo_state)
      room:sortByAction(tos)
      for _, to in ipairs(tos) do
        if state == fk.SwitchYang then
          LB_U.removeVirtualEquip(to, "eight_diagram")
        else
          if to:isAlive() then
            local pattern = ".|2~9|spade"
            local judge = {
              who = to,
              reason = "lightning",
              pattern = pattern,
            }
            room:judge(judge)
            if judge:matchPattern() then
              room:damage {
                to = to,
                damage = 3,
                damageType = fk.ThunderDamage,
                skillName = skel.name,
              }
            end
          end
        end
      end
      if not use.extraUse then
        local red, black = 0, 0
        for _, id in ipairs(DIY.getCenterCards(room)) do
          local card = Fk:getCardById(id)
          if card.color == Card.Red then
            red = red + 1
          elseif card.color == Card.Black then
            black = black + 1
          end
        end
        if red == black then
          player:addCardUseHistory(use.card.trueName, -1)
          use.extraUse = true
        end
      end
    end
  end,
  enabled_at_response = function(self, player, response)
    return not response
  end,
})

--[[
skel:addEffect("targetmod", {
  extra_target_func = function(self, player, skill, card)
    if card and table.contains(card.skillNames, skel.name) then
      return 999
    end
  end,
})
--]]
skel:addAcquireEffect(function(self, player, is_start)
  player.room:addSkill("#CenterArea")
end)


return skel
